<?
class WTree extends Athena 
{
	function GetAllType()
	{
		$arr		= array();
		$type_row	= array();
		$row		= array();
		//判断是否存在VIEW字段
		$fields = mysql_list_fields(DB_NAME,$this->table);
		$columns = mysql_num_fields($fields);
		for ($i = 0; $i < $columns; $i++) 
			array_push($row,mysql_field_name($fields,$i));
		if(array_search('view',$row))
		{
			$sql = "select id,id ids,name,view,rootid from {$this->table} order by rootid,id";
			$query = $this->db->query($sql);
			//$key	= array_values($this->list_field);
			$key = array('id','ids','name','view','rootid');
			//字段总数
			$len = count($key);
			while(($row = mysql_fetch_array($query))==true)
			{
				$val = array();
				for($i=0;$i<$len;$i++)
				{
					if($i>1 && $this->struct[$key[$i]]['Field_Option_Sql'] != "" && $i < $len-1)
						array_push($val,$this->MicroCode_Relation[$key[$i]][$row[$key[$i]]]);
					else
						array_push($val,$row[$i]);
				}
				array_push($arr,array_combine($key,$val));
			}
		}
		else
		{
			$sql = "select id,id ids,name,rootid from {$this->table} order by rootid,id";
			$arr = $this->db->query_row($sql,MYSQL_ASSOC);
		}
		$this->RecursiveType($type_row,$arr);
		return array_slice($type_row,$this->start,$this->pagesize);
	}
	function RecursiveType(& $type_row,& $arr,$rootid=0,$deep = 0)
	{
		for($i=0;$i<count($arr);$i++)
		{
			if($arr[$i]['rootid'] == $rootid)
			{
				$arr[$i]['name'] = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$deep) . $arr[$i]['name'];
				$arr[$i]['deep'] = $deep;
				$tmp = array_slice($arr,$i,1,true);
				
				$type_row = array_merge($type_row,$tmp);
				
				$this->RecursiveType($type_row,$arr,$arr[$i]['id'],$deep+1);
			}
		}
	}
	function Save_Add($str_key="",$str_value="")
	{
		foreach($this->struct as $key => $val)
		{
			if($this->struct[$key]['Field_Add'])
			{
				//组合字段和值
				$str_key	.= ",`$key`";
				if($this->struct[$key]['Field_View'])
				{
					if ($this->struct[$key]['Form_Type'] == "password")
						$str_value	.= ",'".md5($_POST[$this->struct[$key]['Field_Name']])."'";
					else
					{
						if($this->struct[$key]['Form_Type'] == "checkbox")
						{
							$val = $this->Request_Multiple($this->struct[$key]['Field_Name']);
							$str_value .= ",'$val'";
						}
						else 
						{//print($_POST[$this->struct[$key]['Field_Name']]."<br>");
							if($this->struct[$key]['Form_Editor'])
								$str_value .= ",'{$_POST[$this->struct[$key]['Field_Name']]}'";
							else 
								$str_value .= ",'". htmlspecialchars($_POST[$this->struct[$key]['Field_Name']],ENT_QUOTES)."'";
						}
					}
				}
				else
					$str_value	.= ",'{$this->struct[$key]['Form_Default']}'";
			}
		}

		if($str_key == "")
			$this->halt("没有设置任何新增字段，执行插入语句失败！");
		//删除第一个“,"
		$str_key		= trim($str_key,",");//substr($str_key,1);
		$str_value		= trim($str_value,",");//substr($str_value,1);
		$sql = "insert into `".$this->table . "`($str_key) values($str_value)";
		$this->db->query($sql);
		$id		= mysql_insert_id();
		$this->Resort();
		if(isset($_POST['Athena_Continue_Add']) && $_POST['Athena_Continue_Add'] == 1)
			$this->halt("新增<font color=red> </font>成功！",ATHENA_SCRIPT_INDEX."?cmd=add&".$this->parse_QueryString('cmd'));
		else
			$this->halt("新增<font color=red> </font>成功！",$_COOKIE["SYSTEM_HTTP_REFERER"]);
	}
	function Resort()
	{
		$sql = "select id,id ids,name,rootid,deep from {$this->table} order by rootid,idx";
		$arr = $this->db->query_row($sql,MYSQL_ASSOC);
		$type_row = array();
		$this->RecursiveType($type_row,$arr);//print_r($arr);exit;
		for($i=0;$i<count($type_row);$i++)
		{
			$sql = "update {$this->table} set idx=$i,deep={$type_row[$i]['deep']} where id={$type_row[$i]['id']}";
			$this->db->query($sql);
		}
	}
	function View_Data()
	{
		if(isset($this->MicroCode_Relation["rootid"][0]))
			$this->MicroCode_Relation["rootid"][0] = "";
		parent::View_Data();
		
	}
	function  View_Data_Sql($str_search_where)
	{
		$sql 				= "select `{$this->primary}`";
		foreach($this->struct as $key => $val)
		{
			if($this->struct[$key]['Form_View'])
			{
				if(strtolower($key) == "name")
					$sql .= ",concat(REPEAT('&nbsp;&nbsp;&nbsp;&nbsp;',deep),name) as name";
				else 
					$sql .= ",`{$this->struct[$key]['Field_Name']}`";
			}
		}
		$sql = "$sql from `{$this->table}` $str_search_where order by `{$this->order_by_field}` {$this->order} limit {$this->start},{$this->pagesize}";
		return $sql;
	}
	function SaveSort($idx)
	{
		$arr = split(",",$idx);
		for($i=0;$i<count($arr);$i++)
		{
			$this->db->query("update {$this->table} set idx=" . ($i+1) . " where Id=" . $arr[$i]);
			//print ("update {$this->table} set idx=" . ($i+1) . " where Id=" . $arr[$i]);
		}
		$this->Resort();
	}
	
}

?>